package myJs.user.workflow.featureSelection.preprocess

import com.softwaremill.quicklens._
import myJs.Implicits._
import myJs.cps._

import AllCp.PreprocessT
import shared.pojo.WorkflowMission._
import myJs.myPkg.jquery._
import myJs.myPkg.reactHookForm._
import myJs.tool.Tool
import myJs.tool.Tool._
import myJs.user.FeatureSelectionTool
import shared.Pojo.{EachPreprocessData, FsPreprocessData, RemoveQcT}

import japgolly.scalajs.react.vdom.all._
import japgolly.scalajs.react._
import japgolly.scalajs.react.React.Fragment
import japgolly.scalajs.react.util.EffectCatsEffect._
import myJs.myPkg.Hooks._
import cats.effect._
import myJs.Implicits._
import myJs.Utils._

/** Created by yz on 3/11/2020
  */
object RemoveQcCp extends PreprocessT with RemoveQcT {

  case class Props(
      curI: Int,
      datasChange: List[EachPreprocessData] => SyncIO[Unit],
      datas: List[EachPreprocessData],
      insertModule: String => SyncIO[Unit],
      removeModule: Int => SyncIO[Unit],
      extraData: ExtraData
  ){
    @inline def render: VdomElement = {
      Component(this)
    }
  }

  val Component = ScalaFnComponent[Props] { props =>
    object FTool {

      def removeGroupsChange = (v: List[String]) => {
        val newPreProcess = props.datas.modify(_.index(props.curI).removeGroups).setTo(v)
        props.datasChange(newPreProcess)
      }

      def removeModuleF =  {
        props.removeModule(props.curI)
      }

      def insertModule = (moduleName: String) => {
        props.insertModule(moduleName)
      }

    }

    useEffect(
      SyncIO{
        Tool.popover
      }.void,
      List()
    )

    val data = props.datas(props.curI)

    useEffect(
      {
        val groups = props.extraData.groups
        val newVs = data.removeGroups.filter { x =>
          groups.contains(x)
        }
        FTool.removeGroupsChange(newVs)
      },
      List(props.extraData.groups)
    )

    val groupMap = props.extraData.groups.optionMap

    val removeGroups = data.removeGroups
    val namePrefix = s"preprocess.datas.${props.curI}"

    fieldset(
      paddingRight := "5px", border := "1px solid silver", margin := "0 2px", borderRadius := "4px",
      className := "northbtn myEachSoft",
      MyToolCp.Props(removeModule = FTool.removeModuleF, insertModule = FTool.insertModule).render,
      legend(s"${messages(nameStr)}"),
      RSelectMulCp.Props(
        value = removeGroups,
        label = messages("removeGroups"),
        name = s"${namePrefix}.removeGroups",
        optionMap = groupMap,
        onChange = FTool.removeGroupsChange
      ).render
    )

  }

  def cp(
      curI: Int,
      datas: List[EachPreprocessData],
      datasChange: List[EachPreprocessData] => SyncIO[Unit],
      insertModule: String => SyncIO[Unit],
      removeModule: Int => SyncIO[Unit],
      extraData: ExtraData
  ) = {
    RemoveQcCp.Props(
      curI = curI,
      datas = datas,
      datasChange = datasChange,
      insertModule = insertModule,
      removeModule = removeModule,
      extraData = extraData
    ).render
  }

}
